<template>
  <div style="height: 100%;">
    <!--flex弹性盒子模型，justify-content：主抽 -->
    <div class="title">丰歌网络科技有限公司综合信息管理系统</div>
    <div style="display: flex;justify-content: center;margin-top: 90px">
      <el-card style="width: 400px">
        <div slot="header" class="clearfix">
          <span>登录</span>
        </div>
        <table>
          <tr>
            <td>用户名</td>
            <td>
              <el-input v-model="user.username" placeholder="请输入用户名" clearable></el-input>
            </td>
          </tr>
          <tr>
            <td>密码</td>
            <td>
              <el-input
                type="password"
                v-model="user.password"
                placeholder="请输入密码"
                @keydown.enter.native="doLogin"
                clearable
              ></el-input>
            </td>
          </tr>
          <tr>
            <td colspan="2">
              <el-button style="width: 300px" type="primary" @click="doLogin">登录</el-button>
            </td>
          </tr>
        </table>
      </el-card>
    </div>
    <div class="beian-login">
      <div>©2020 上海丰歌网络科技有限公司</div>
      <div><a href="http://beian.miit.gov.cn/" target="_blank">沪ICP备18015403号-1</a></div>
    </div>
  </div>
</template>
<script>
import Vue from 'vue'

export default {
  data () {
    return {
      user: {
        username: 'sunchang',
        password: 'sunchang'
        // 为了登录方便，可以直接在这里写好用户名和密码的值
      }
    }
  },
  methods: {
    doLogin () {
      const loading = this.$loading({
        lock: true,
        text: 'Loading',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      })
      // 一点击登录按钮，这个方法就会执行
      this.$http.post(`http://${process.env.NODE_ENV === 'development' ? 'localhost:9009' : 'qiucc.com'}/rentmall/login`, {
        query: {},
        form: this.user,
        pagination: {}
      }).then(res => {
        if (res.data.statusCode !== 200) {
          this.$message.error(res.data.message)
          return
        }
        Vue.prototype.userInfo = res.body.userInfo
        Vue.prototype.shopInfo = res.body.shopInfo
        localStorage.setItem('userInfo', JSON.stringify(res.body.userInfo))
        localStorage.setItem('shopInfo', JSON.stringify(res.body.shopInfo))
        this.$router.replace({
          path: `/home`
        })
      }).catch(err => {
        console.error('登录失败', err)
      }).finally(() => {
        loading.close()
      })
    }
  }
}
</script>
<style>
.title {
  font-size: 30px;
  font-weight: bold;
  text-align: center;
  padding-top: 80px;
  line-height: 1;
  color: #589ef8;
}
.beian-login {
  position: absolute;
  width: 100%;
  bottom: 0;
  padding: 5px 0;
  text-align: center;
  font-size: 12px;
  color: #666666;
}
</style>
